/**********************************************************************
 *@Project： clouddemo
 *@File: UserRealm
 *@Date: 2017/4/15
 *@Copyright (C) 2013-2017 http://www.posun.cn. All Rights Reserved.
 ***********************************************************************
 *注意： 本内容仅限于深圳市普盛实业有限公司内部使用，禁止外泄
 **********************************************************************
 */
package com.posun.edu.system.config;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.posun.edu.system.entity.User;
import com.posun.edu.system.service.IUserService;
import com.posun.framework.util.WebUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * TODO<br>
 *
 * @author YL
 * @version 1.0
 * @createTime 2017/4/15 16:27
 * @ChangeLog
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private IUserService userService;

    public UserRealm() {
        super();
        setAuthenticationTokenClass(UsernamePasswordToken.class);
    }

    @Override
    public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("==================================执行Shiro权限认证=========================================");
        UsernamePasswordToken token=(UsernamePasswordToken) authenticationToken;
        System.out.println("验证当前Subject时获取到token为：" + token);

        //查出是否有此用户
        User su=new User();
        su.setId("admin");
        su.setUserName("admin");
        //到数据库查是否有此对象
/*        User user=userService.findObject(token.getUsername());
        if(user == null){
            throw new UnknownAccountException();//账户不存在
        }*/
        List<Object> principals = Lists.newArrayList();
        principals.add(su.getUserName());
        PrincipalCollection principalCollection = new SimplePrincipalCollection(principals, getName());
        WebUtil.saveCurrentUser(su);

        return new SimpleAuthenticationInfo(principalCollection,token.getPassword());
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(Sets.newHashSet("*"));
        info.setRoles(Sets.newHashSet("superAdmin"));
        return info;
    }
}
